package cn.ithers.blog.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.ithers.blog.dao.CategoryDao;
import cn.ithers.blog.dto.CategoryDTO;
import cn.ithers.blog.pojo.Category;
import cn.ithers.blog.pojo.User;
import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.ICategoryService;
import cn.ithers.blog.service.IUserService;
import cn.ithers.blog.utils.Constants;
import cn.ithers.blog.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhanghuan
 * @date: 2020/9/28 9:44
 * @description:
 */
@Slf4j
@Service
@Transactional
public class CategoryServiceImpl extends BaseService implements ICategoryService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private IUserService userService;

    /**
     * 添加分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public ResponseResult addCategory(CategoryDTO categoryDTO) {
        //检查数据
        //必须的数据：分类名称，分类的拼音，顺序，描述
        String name = categoryDTO.getName();
        if (StrUtil.isEmpty(name)) {
            return ResponseResult.FAILED("分类名称不能为空");
        }
        String pinyin = categoryDTO.getPinyin();
        if (StrUtil.isEmpty(pinyin)) {
            return ResponseResult.FAILED("分类拼音不能为空");
        }
        String description = categoryDTO.getDescription();
        if (StrUtil.isEmpty(description)) {
            return ResponseResult.FAILED("分类描述不能为空");
        }
        //补全数据
        //补全id
        Category category = new Category();
        category.setName(name);
        category.setPinyin(pinyin);
        category.setDescription(description);
        category.setId(idWorker.nextId() + "");
        category.setStatus("1");
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        log.info("------------>" + category.toString());
        //保存数据
        categoryDao.save(category);
        return ResponseResult.SUCCESS("添加分类成功");
    }

    /**
     * 获取分类
     *
     * @param categoryId
     * @return
     */
    @Override
    public ResponseResult getCategory(String categoryId) {
        Category category = categoryDao.findOneById(categoryId);
        if (category == null) {
            return ResponseResult.FAILED("分类不存在");
        }
        return ResponseResult.SUCCESS("获取分类成功").setData(category);
    }

    /**
     * 获取分类列表
     *
     * @return
     */
    @Override
    public ResponseResult getCategoryList() {
        log.info("----------->进入获取分类列表");
        //创建条件
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime", "order");
        //判断用户角色 普通用户/或者未登录的用户只能获取正常的分类
        //管理员账户可以拿到所有的分类
        User user = userService.checkUser();
        List<Category> categories;
        if (user == null || !Constants.User.ROLE_ADMIN.equals(user.getRoles())) {
            //只能读取到正常的分类
            categories = categoryDao.listCategoriesByStatus("1");
        } else {
            categories = categoryDao.findAll(sort);
        }
        return ResponseResult.SUCCESS("获取分类列表成功").setData(categories);

    }

    /**
     * 修改分类
     *
     * @param categoryId
     * @param categoryDTO
     * @return
     */
    @Override
    public ResponseResult updateCategory(String categoryId, CategoryDTO categoryDTO) {
        //1.找出来
        Category categoryFromDb = categoryDao.findOneById(categoryId);
        if (categoryFromDb == null) {
            return ResponseResult.FAILED("分类不存在");
        }
        //2.对内容判断，有些字段不可以为空
        String name = categoryDTO.getName();
        if (!StrUtil.isEmpty(name)) {
            categoryFromDb.setName(name);
        }
        String pinyin = categoryDTO.getPinyin();
        if (!StrUtil.isEmpty(pinyin)) {
            categoryFromDb.setPinyin(pinyin);

        }
        String description = categoryDTO.getDescription();
        if (!StrUtil.isEmpty(description)) {
            categoryFromDb.setDescription(description);
        }
        categoryFromDb.setOrder(categoryDTO.getOrder());
        categoryFromDb.setUpdateTime(new Date());
        //3.保存数据
        categoryDao.save(categoryFromDb);
        //返回结果
        return ResponseResult.SUCCESS("修改分类成功");
    }

    /**
     * 删除
     *
     * @param categoryId
     * @return
     */
    @Override
    public ResponseResult deleteCategory(String categoryId) {
        int result = categoryDao.deleteCategroyByUpdateState(categoryId);
        if (result == 0) {
            return ResponseResult.FAILED("该分类不存在");
        }
        return ResponseResult.SUCCESS("删除分类成功");
    }
}
